<?php
class Media extends Dao
{
	private static $_instance = null;
	protected $_db = null, $_cache = null;
	
	protected function __construct()
	{
		parent::__construct();
	}
	
	public static function getInstance() 
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
	
	public function getList(array $args = array(), $intPage = 1, $intLimit = MIN_ROWS_PER_PAGE)
	{
		try{
			$defaults = array(
				'sort' => null,
				'keyword' => null,
				'file_type' => null,
				'created_from' => null,
				'created_to' => null,
				'folder' => null
			);
			$args = $this->args_merge($defaults, $args, true);			

			$intStart = ($intPage-1)*$intLimit;
			
			$strSql = " 	
						SELECT SQL_CALC_FOUND_ROWS * 
						FROM tbl_cms_media
						WHERE media_folder <=> IFNULL(:folder, media_folder) 
							  AND FIND_IN_SET(media_file_extension, IFNULL(:file_type, media_file_extension))
							  AND media_title LIKE IFNULL(:keyword, media_title)
							  AND DATE(media_created_date) BETWEEN DATE(IFNULL(:startDate, media_created_date)) AND DATE(IFNULL(:endDate, media_created_date))
						ORDER BY media_id DESC
						LIMIT :intStart, :intLimit
					  ";
			$cmd = $this->_db->createCommand($strSql);	
			$cmd->bindParam(":folder",$args['folder'],PDO::PARAM_INT);
			$cmd->bindParam(":file_type",$args['file_type'],PDO::PARAM_INT);
			$cmd->bindParam(":keyword",$args['keyword'],PDO::PARAM_INT);
			$cmd->bindParam(":startDate",$args['created_from'],PDO::PARAM_STR);
			$cmd->bindParam(":endDate",$args['created_to'],PDO::PARAM_STR);
			$cmd->bindParam(":intStart",$intStart,PDO::PARAM_INT);
			$cmd->bindParam(":intLimit",$intLimit,PDO::PARAM_INT);
			$arrResults = $cmd->queryAll();
			if(!empty($arrResults)){
				$arrResults[0]['total_rows'] = $this->_db->createCommand('SELECT FOUND_ROWS();')->queryScalar();
			}
			return $arrResults;
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function get($id)
	{
		if($id === null || $id == '') throw new Exception('Invalid id!');
		try{
			$strSql = " SELECT *
						FROM tbl_cms_media
						WHERE media_id=:id";
			$cmd = $this->_db->createCommand($strSql);
			$cmd->bindParam(":id",$id,PDO::PARAM_STR);
			return $arrResults = $cmd->queryRow();
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function insert(array $args)
	{
		try{
			$defaults = array(
				'media_title' => null,
				'media_description' => null,
				'media_file' => null,
				'media_file_size' => null,
				'media_file_extension' => null,
				'media_alt' => null,
				'media_creator_id' => null,
				'media_created_date' => new CDbExpression('NOW()'),
				'media_src' => 1,
				'media_folder' => null
			);
			$args = $this->args_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			$cmd->insert('tbl_cms_media',$args);
			return Yii::app()->db->getLastInsertId();
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function delete($id)
	{
		try{
			$cmd = $this->_db->createCommand();
			return $cmd->delete('tbl_cms_media', 'media_id=:id', array(':id'=>$id));		
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
}